<?php

// | Author: 阳顶天 <yangjie@wttai.com>
// +----------------------------------------------------------------------
namespace Card\Controller;


/**
 * API控制器
 * @author  阳顶天 <yangjie@wttai.com>
 */
class MobileController extends CardController {
   
   
   /*
    * H5图集首页数据获取
    * @author  阳顶天 <yangjie@wttai.com>
    * @param type $category  分类
    * @param type $p         页码
    */
    public function index(){

        if($this->_method != 'post'){
            $this->ajaxReturn(errorReturn('请用POST访问！'));
        }

        $page = intval(I('post.p',1,'int'));
        $category = intval(I('post.category',1,'int'));
        
        if ($page >1){
            $member_id = I('post.member_id');
            $check_member = D('Member')->query_ums_member_bay($member_id);
            if ($check_member === false){
                //$page = 1;
                $this->ajaxReturn($return = array(
                    'code' => 1001,
                    'desc' => '请先登陆'
                ));
            }
        }
        
        $row = intval(I('post.row',30,'int'));
        $title = I('post.title','');
        $page = $page ? $page : 1; //默认显示第一页数据
        //获取素材标签
        $category_list = M('category_items')->where(['status'=>1])->order(['m_displayorder'=>'desc','id'])->select();
        foreach ($category_list as $cur_category){
            $cur_category['id'] == $category && $comefrom = $cur_category['title'];
        }
        
        $result['comefrom'] = $comefrom;
        $result['category_list'] = $category_list; 

        if ($title == TRUE){
            $this->ajaxReturn(successReturn($result));
        }
        
        //获取字段
        $fields= ['id','img','title','createtime','city','width','height','s_url','t_url','brand','class'];
        $map['status'] = 1;
        $map['status'] = 1;
        $map['m_status'] = 1;
        $map['category'] = $category;
        $data = M('listitems')
                /* 查询指定字段，不指定则查询所有字段 */
                ->field($fields)
                // 查询条件
                ->where($map)
                /* 默认通过id逆序排列 */
                ->order(['m_displayorder'=>'desc','id'])
                /* 数据分页 */
                ->page($page, $row)
                /* 执行查询 */
                ->select();

            /* 查询记录总数 */
            $count = M('listitems')->where($map)->count();
            
        $result['mydata'] = $data; 
        $result['count'] = $count; 
        $result['page'] = $page; 
        $this->ajaxReturn(successReturn($result));
    }

    
    /*
    * H5图集子集
    * @author  阳顶天 <yangjie@wttai.com>
    * @param type $id       图集id
    * @param type $p         页码
    */
    public function gallery(){
 
        if($this->_method != 'post'){
            $this->ajaxReturn(errorReturn('请用POST访问！'));
        }
        
        $id = I('post.id',0,'int');
        $row = intval(I('post.row',30,'int'));
        $page = intval(I('post.p',1,'int'));        
        empty($id) && $this->ajaxReturn(errorReturn('缺少id'));
        
        if ($page >1){
            $member_id = I('post.member_id');
            $check_member = D('Member')->query_ums_member_bay($member_id);
            if ($check_member === false){
                //$page = 1;
                $this->ajaxReturn($return = array(
                    'code' => 1001,
                    'desc' => '请先登陆'
                ));
            }
        }

        
        //获取城市信息
        $listitems = M('listitems')->where(['id'=>$id])->field(['city','title','category','brand','class'])->limit(1)->select();
        empty($listitems) &&  $this->error('无分类数据');
        $category_items = M('category_items')->where(['id'=>$listitems[0]['category']])->field(['title','id'])->limit(1)->select();

        //获取字段
        $fields= ['id','img','title','createtime','t_url','image','s_url','t_url','width','height'];
        $map['status'] = 1;
        $map['m_status'] = 1;
        $map['category'] = $category;
        $map['cat_code'] = $id;
        $data = M('items')
                /* 查询指定字段，不指定则查询所有字段 */
                ->field($fields)
                // 查询条件
                ->where($map)
                /* 默认通过id逆序排列 */
                ->order(['m_displayorder'=>'desc','id'])
                /* 数据分页 */
                ->page($page, $row)
                /* 执行查询 */
                ->select();

            /* 查询记录总数 */
            $count = M('items')->where($map)->count();
        
        $result['mydata'] = $data; 
        $result['listitems'] = $listitems; 
        $result['category_items'] = $category_items; 
        $result['count'] = $count; 
        $result['page'] = $page; 
        $this->ajaxReturn(successReturn($result));
        
    }
    

    /*
    * H5图片详情
    * @author  阳顶天 <yangjie@wttai.com>
    * @param type $id        图片
    * @param type $p         页码
    */
    public function gallery_detail(){
        
        if($this->_method != 'post'){
            $this->ajaxReturn(errorReturn('请用POST访问！'));
        }
        $detail_id = I('post.id',0,'int');
        empty($detail_id) && $this->ajaxReturn(errorReturn('缺少id'));
        
        $fields = ['id','img','image','s_url','t_url','title','createtime'];
        $data = M('items')->where(['id'=>$detail_id,'status'=>1,'pc_status'=>1])->getBYfield($fields);       
        
        //获取导航
        $items_id = M('items')->where(['id'=>$detail_id])->getfield('cat_code');
        empty($items_id) && $this->ajaxReturn(errorReturn('无分类数据哦'));
        $listitems = M('listitems')->where(['id'=>$items_id])->field(['id,city','title','category'])->limit(1)->select();
        empty($listitems) &&  $this->ajaxReturn(errorReturn('无分类数据'));
        $category_items = M('category_items')->where(['id'=>$listitems[0]['category']])->field(['title','id'])->limit(1)->select();

        $result['mydata'] = $data; 
        $result['listitems'] = $listitems; 
        $result['category_items'] = $category_items; 

        $this->ajaxReturn(successReturn($result));

    }
}
